using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Org.Loon.Framework.Xna.Game.Core;
using Org.Loon.Framework.Xna.Game.Core.Timer;
using Org.Loon.Framework.Xna.Game.Core.Graphics.OpenGL;
using Org.Loon.Framework.Xna.Game.Core.Geom;

namespace Org.Loon.Framework.Xna.Game.Action.Sprite.Effect
{
    public class ZoomEffect : LObject, ISprite
    {

        private const long serialVersionUID = 1L;

        private int width, height;

        private bool visible, complete;

        private LTimer timer;

        private LTexture texture;

        private int count;

        private int maxcount;

        private int centerx, centery;

        public ZoomEffect(String fileName):this(new LTexture(fileName))
        {
            
        }

        public ZoomEffect(LTexture t)
        {
            this.maxcount = 20;
            this.centerx = -1;
            this.centery = -1;
            this.texture = t;
            this.width = t.GetWidth();
            this.height = t.GetHeight();
            this.timer = new LTimer(100);
            this.visible = true;
        }

        public void SetDelay(long delay)
        {
            timer.SetDelay(delay);
        }

        public long GetDelay()
        {
            return timer.GetDelay();
        }

        public bool IsComplete()
        {
            return complete;
        }

        public override int GetHeight()
        {
            return height;
        }

        public override int GetWidth()
        {
            return width;
        }

        public override void Update(long elapsedTime)
        {
            if (complete)
            {
                return;
            }
            if (this.count >= this.maxcount)
            {
                this.complete = true;
            }
            if (timer.Action(elapsedTime))
            {
                count++;
            }
        }

        public void CreateUI(GLEx g)
        {
            if (!visible)
            {
                return;
            }
            if (complete)
            {
                if (alpha > 0 && alpha < 1)
                {
                    g.SetAlpha(alpha);
                }
                g.DrawTexture(this.texture, X(), Y(), width, height);
                if (alpha > 0 && alpha < 1)
                {
                    g.SetAlpha(1f);
                }
                return;
            }
            if (alpha > 0 && alpha < 1)
            {
                g.SetAlpha(alpha);
            }
            if (this.centerx < 0)
            {
                this.centerx = (width / 2);
            }
            if (this.centery < 0)
            {
                this.centery = (height / 2);
            }
            if (this.count < this.maxcount / 2)
            {
                float partxs = this.centerx / (this.maxcount / 2 + 1);
                float partys = this.centery / (this.maxcount / 2 + 1);
                float partxe = (width - this.centerx)
                        / (this.maxcount / 2 + 1);
                float partye = (height - this.centery)
                        / (this.maxcount / 2 + 1);
                int dxs = (int)(this.count * partxs);
                int dys = (int)(this.count * partys);
                int dxe = (int)(width - this.count * partxe);
                int dye = (int)(height - this.count * partye);
                g.DrawTexture(this.texture, X(), Y(), width, height, dxs, dys, dxe,
                        dye);
            }
            else
            {
                float partxs_0 = this.centerx / (this.maxcount / 2 + 1);
                float partys_1 = this.centery / (this.maxcount / 2 + 1);
                float partxe_2 = (width - this.centerx)
                        / (this.maxcount / 2 + 1);
                float partye_3 = (height - this.centery)
                        / (this.maxcount / 2 + 1);
                int dxs_4 = (int)((this.maxcount - this.count) * partxs_0);
                int dys_5 = (int)((this.maxcount - this.count) * partys_1);
                int dxe_6 = (int)(width - (this.maxcount - this.count) * partxe_2);
                int dye_7 = (int)(height - (this.maxcount - this.count) * partye_3);
                g.DrawTexture(this.texture, X(), Y(), width, height, dxs_4, dys_5, dxe_6,
                        dye_7);
            }
            if (alpha > 0 && alpha < 1)
            {
                g.SetAlpha(1f);
            }
        }

        public void Reset()
        {
            this.complete = false;
            this.count = 0;
        }

        public LTexture GetBitmap()
        {
            return texture;
        }

        public RectBox GetCollisionBox()
        {
            return GetRect(X(), Y(), width, height);
        }

        public int GetCenterX()
        {
            return centerx;
        }

        public void SetCenterX(int centerx_0)
        {
            this.centerx = centerx_0;
        }

        public int GetCenterY()
        {
            return centery;
        }

        public void SetCenterY(int centery_0)
        {
            this.centery = centery_0;
        }

        public int GetMaxCount()
        {
            return maxcount;
        }

        public void SetMaxCount(int maxcount_0)
        {
            this.maxcount = maxcount_0;
        }

        public bool IsVisible()
        {
            return visible;
        }

        public void SetVisible(bool visible_0)
        {
            this.visible = visible_0;
        }

        public void Dispose()
        {
            if (texture != null)
            {
                texture.Destroy();
                texture = null;
            }
        }
    }
}
